//
//  TGHttpRequest.m
//  GroupBuy
//
//  Created by wwj on 15/11/10.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "TGHttpRequest.h"
#import "TGDataTool.h"
#import "TGDealModel.h"
#import "TGOrderModel.h"
#import "HttpUtil.h"
#import "TGLocationTool.h"


@implementation TGHttpRequest

+(void)getgroupListWithPosition:(CLLocationCoordinate2D)position success:(SuccessBlock)success error:(ErrorBlock)error{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    TGCityModel *city = [TGDataTool shareDataTool].currentCity;
    if (city == nil) {
        return;
    }
    [params setObject:[TGDataTool shareDataTool].currentCity.name forKey:@"city"];
    [params setObject:@(position.longitude) forKey:@"longitude"];
    [params setObject:@(position.latitude) forKey:@"latitude"];
    
    [self getGroupListWithParams:params success:success error:error];
}

#pragma -mark  获取分类元数据
+(void)getCategoriesWithSuccess:(SuccessBlock)success error:(ErrorBlock)error{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    [[HttpUtil shareHttpUtil] requestWithURL:@"v1/metadata/get_categories_with_deals" params:params block:^(id result, NSError *errorObj) {
        if (result) {
            if (success) {
                
            }
        }else if (error){
            error(errorObj);
        }
    }];
}



#pragma -mark 获取团购列表数据
+(void)getGroupListWithPage:(int)page success:(SuccessBlock)success error:(ErrorBlock)error{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    [params setObject:[TGDataTool shareDataTool].currentCity.name forKey:@"city"];
    
    NSString *district = [TGDataTool shareDataTool].currentDistrict;
    if (district && ![kAllDistrictStr isEqualToString:district]) {
        [params setObject:district forKey:@"region"];
    }
    
    NSString *category =[TGDataTool shareDataTool].currentCategory;
    if (category && ![kCategoryStr isEqualToString:category]) {
        [params setObject:category forKey:@"category"];
    }
    
    TGOrderModel *order = [TGDataTool shareDataTool].currentOrder;
    if (order) {
        if (order.index == 7) {// 按距离最近排序
            TGCityModel *city = [TGLocationTool shareLocationTool].locationCity;
            if (city) { // 按距离最近排序
                [params setObject:@(order.index) forKey:@"sort"];
                // 多增加经纬度参数
                [params setObject:@(city.position.latitude) forKey:@"latitude"];
                [params setObject:@(city.position.longitude) forKey:@"longitude"];
            }
        } else { // 按照其他方式排序
            [params setObject:@(order.index) forKey:@"sort"];
        }
    }
    
    if (page >0) {
        [params setObject:@(page) forKey:@"page"];
    }
    
    [self getGroupListWithParams:params success:success error:error];
}

+(void)getGroupListWithParams:(NSDictionary *)params success:(SuccessBlock)success error:(ErrorBlock)error{
    [[HttpUtil shareHttpUtil] requestWithURL:@"v1/deal/find_deals" params:params block:^(id result, NSError *errorObj) {
        if (success) {
            NSArray *dictArray = result[@"deals"];
            NSMutableArray *dataArray = [NSMutableArray array];
            for (NSDictionary *dict in dictArray) {
                TGDealModel *model = [TGDealModel dealModelWithDict:dict];
                [dataArray addObject:model];
            }
            NSString *totalCount = result[@"total_count"];
            success(dataArray, @(totalCount.intValue));
        }else if(error){
            if (errorObj) {
                error(errorObj);
            }
        }
    }];
}

#pragma mark 获取团购详情
+(void)getGroupDetailWithDealId:(NSString *)dealID success:(SuccessBlock)success error:(ErrorBlock)error{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:dealID forKey:@"deal_id"];
    [[HttpUtil shareHttpUtil] requestWithURL:@"v1/deal/get_single_deal" params:params block:^(id result, NSError *errorObj) {
        if (result) {
            if (success) {
                TGDealModel *model = [TGDealModel dealModelWithDict:result[@"deals"][0]];
                success(model, nil);
            }
        }else if (error){
            error(errorObj);
        }
    }];
}

@end
